$(function () {
    $('#add-on-update').click(function () {
        $('#add-auto-update-input').show();
        $('#add-auto-update-label').show();
        $('#addTime').prop('checked',true);
    })
    $('#add-off-update').click(function () {
        $('#add-auto-update-input').hide();
        $('#add-auto-update-label').hide();
        $('#addTime').prop('checked',false);
    })

    $('#edit-on-update').click(function () {
        $('#edit-auto-update-input').show();
        $('#edit-auto-update-label').show();
        $('#editTime').prop('checked',true);
    })
    $('#edit-off-update').click(function () {
        $('#edit-auto-update-input').hide();
        $('#edit-auto-update-label').hide();
        $('#editTime').prop('checked',false);
    })
    $('#addNoDiscount').click(function () {
        $('#addDiscountRate').attr('disabled',true);
    })
    $('#addHasDiscount').click(function () {
        $('#addDiscountRate').attr('disabled',false);
    })
    $('#editNoDiscount').click(function () {
        $('#editDiscountRate').attr('disabled',true);
    })
    $('#editHasDiscount').click(function () {
        $('#editDiscountRate').attr('disabled',false);
    })
    window.vipListNum = $("#tag-list").children().length-1;
    if(window.vipListNum=='8'){
        $("#div-add-vip").remove();
    }else{
        $("#div-add-vip").show();
    }
    
    //升级校验
    var checkLevelRule = function (level, levelRule, btnMe, btnInfo) {
        if(levelRule == ""){
            btnInfo.text(lang('vip.upgrade'));
            return false;
        }else if(level==='1'){
            if(!window.commonRegexp.roomTypePrice.test(levelRule)){
                btnInfo.text(lang('vip.decimals'));
                return false;
            }
        }else{
            if(!/^[\d]{1,8}$/.test(levelRule)){
                btnInfo.text(lang('vip.positive'));
                return false;
            }
            btnMe.val(parseInt(levelRule));
            levelRule = parseInt(levelRule);
        }
    }
    //优惠校验
    var checkDiscountRule = function (discount, discountRule, btnMe, btnInfo) {
        if(discountRule == ""){
            btnInfo.text(lang('vip.style'));
            return false;
        }else if(discount==='1'){
            if(!window.commonRegexp.roomTypePrice.test(discountRule)){
                btnInfo.text(lang('vip.decimals'));
                return false;
            }
        }else{
            var discount = /^(\d(\.[\d]{1,2})?|10(\.[0]{1,2})?)$/;
            if(!discount.test(discountRule)){
                btnInfo.text(lang('vip.discountTip'));
                return false;
            }
        }
    }

    //升级规则校验
    $('#addLevelRule').blur(function () {
        $('#add-vip-level-info').text('');
        var level = $('#add-vip-level').attr('data-level');
        var levelRule = $.trim($(this).val());
        checkLevelRule(level, levelRule, $(this), $('#add-vip-level-info'));
    });

    //优惠规则校验
    $('#addDiscountRate').blur(function () {
        $('#add-vip-level-info').text('');
        var discount = $('#add-vip-level').attr('data-discount');
        var discountRule = $.trim($(this).val());
        checkDiscountRule(discount, discountRule, $(this), $('#add-vip-level-info'));
    });

    //再次点击清空提示
    $('#addVipLevel').click(function () {
        $('#add-vip-level-info').text('');
    });

    //添加等级名称自动校验
    $('#vipLevelModal').inputValidate({
        submit: {
            query: '#add-vip-level'
        },
        inputs: [{
            query: '#addVipLevelName',
            regexp: /^.{1,10}$/,
            message: lang('vip.rank'),
            emptyMessage: lang('vip.rankNull')
        }]
    });
    
    //添加请求
    $('#add-vip-level').on('click', function(e) {
        e.preventDefault();
        var me = $(this);
        var params = {
            hotel: window.currentHotel,
            name: $.trim($('#addVipLevelName').val()),
            auto: $('input:radio[name="add-update-way"]:checked').val(),
            level: me.attr('data-level'),
            discount: me.attr('data-discount'),
            share: me.attr('data-share'),
        };

        //升级方式校验
        if($('#addTime').prop('checked')){
            params.levelRule = $.trim($('#addLevelRule').val());
            var result = checkLevelRule(params.level, params.levelRule, $('#addLevelRule'), $('#add-vip-level-info'));
            if(result === false){
                return false;
            }
        }else{
            params.levelRule = 0;
        }

        //优惠方式校验
        if($('#addHasDiscount').prop('checked')){
            params.discountRule = $.trim($('#addDiscountRate').val());
            var result = checkDiscountRule(params.discount, params.discountRule, $('#addDiscountRate'), $('#add-vip-level-info'));
            if(result === false){
                return false;
            }
        }else if($('#addNoDiscount').prop('checked')){
            params.discountRule=0;
        }

        ajaxRequest('/Home/Customer/addVipLevel', params, 'post', function(data, submitBtn) {
            $('#vipLevelModal').modal('hide');
            var vipInfo = data.data;
            var templateHTML = $($('#templateVipList').clone().html());
            templateHTML.find('.vip-item-left').html(vipInfo.name);
            templateHTML.find('.set-vip-level').attr('data-id',vipInfo.id);
            var vipHTML = templateHTML.prop('outerHTML');
            $('#div-add-vip').before(vipHTML);
            window.vipListNum++;
            if(window.vipListNum=='8'){
                $("#div-add-vip").remove();
            }
        }, function(error, submitBtn, defaultErrorHandler) {
            var errorInfo = error.responseJSON.info;
            $('#add-vip-level-info').text(errorInfo);
        }, me);
    });
    
    $(document).on('click','.set-vip-level', function(e) {
        e.preventDefault();
        var me = $(this);
        var params = {
            hotel: window.currentHotel,
            vipInfoId: $(this).attr('data-id'), 
        };
        ajaxRequest('/Home/Customer/setVipLevel', params, 'post', function(data, submitBtn) {
            var vipInfo = data.data;
            $('#editVipLevelName').val(vipInfo.name);
            if(vipInfo.auto === '1'){
                $('#edit-auto-update-input').show();
                $('#edit-auto-update-label').show();
                $('#edit-on-update').prop('checked',true);
                $('#editTime').prop('checked',true);
                if(vipInfo.level == '0'){
                    $('#editLevelRule').val(parseInt(vipInfo.levelrule));
                }else{
                    $('#editLevelRule').val(vipInfo.levelrule);
                }
            }else{
                $('#edit-off-update').prop('checked',true);
                $('#edit-auto-update-input').hide();
                $('#edit-auto-update-label').hide();
                $('#editTime').prop('checked',false);
            }
            if(vipInfo.discountrule === '0'){
                $('#editNoDiscount').prop('checked',true);
            }else{
                $('#editHasDiscount').prop('checked',true);
                $('#editDiscountRate').val(vipInfo.discountrule);
            }
        }, null, me);
    });

    //修改等级名称自动校验
    $('#setVipLevelModal').inputValidate({
        submit: {
            query: '#edit-vip-level'
        },
        inputs: [{
            query: '#editVipLevelName',
            regexp: /^.{1,10}$/,
            message: lang('vip.rank'),
            emptyMessage: lang('vip.rankNull')
        }]
    });
    //升级规则校验
    $('#editLevelRule').blur(function () {
        $('#edit-vip-level-info').text('');
        var level = $('#add-vip-level').attr('data-level');
        var levelRule = $.trim($(this).val());
        checkLevelRule(level, levelRule, $(this), $('#edit-vip-level-info'));
    });

    //优惠规则校验
    $('#editDiscountRate').blur(function () {
        $('#edit-vip-level-info').text('');
        var discount = $('#add-vip-level').attr('data-discount');
        var discountRule = $.trim($(this).val());
        checkDiscountRule(discount, discountRule, $(this), $('#edit-vip-level-info'));
    });

    //再次点击清空提示
    $(document).on('click','.set-vip-level', function(e) {
        $('#edit-vip-level-info').text('');
        $('#edit-vip-level').data('data-id',$(this).attr('data-id'));
        $('#edit-vip-level').data('vipName',$(this).siblings());
    });

    //修改会员等级请求
    $('#edit-vip-level').on('click', function(e) {
        e.preventDefault();
        var me = $(this);

        var params = {
            hotel: window.currentHotel,
            vipInfoId: me.data('data-id'),
            name: $.trim($('#editVipLevelName').val()),
            auto: $('input:radio[name="edit-update-way"]:checked').val(),
            level: me.attr('data-level'),
            discount: me.attr('data-discount'),
            share: me.attr('data-share'),
        };

        //升级方式校验
        if($('#editTime').prop('checked')){
            params.levelRule = $.trim($('#editLevelRule').val());
            var result = checkLevelRule(params.level, params.levelRule, $('#editLevelRule'), $('#edit-vip-level-info'));
            if(result === false){
                return false;
            }
        }else{
            params.levelRule = 0;
        }

        //优惠方式校验
        if($('#editHasDiscount').prop('checked')){
            params.discountRule = $.trim($('#editDiscountRate').val());
            var result = checkDiscountRule(params.discount, params.discountRule, $('#editDiscountRate'), $('#edit-vip-level-info'));
            if(result === false){
                return false;
            }
        }else if($('#editNoDiscount').prop('checked')){
            params.discountRule=0;
        }

        ajaxRequest('/Home/Customer/editVipLevel', params, 'post', function(data, submitBtn) {
            $('#setVipLevelModal').modal('hide');
            var vipInfo = data.data;
            var vipName = me.data('vipName');
            vipName.text(vipInfo.name);
        }, function(error, submitBtn, defaultErrorHandler) {
            var errorInfo = error.responseJSON.info;
            $('#edit-vip-level-info').text(errorInfo);
        }, me);
    });

    //确认初始化
    $('#confirmVipInit').click(function () {
        var shareWay = $('input:radio[name="shareWay"]:checked').val();
        var levelWay = $('input:radio[name="levelWay"]:checked').val();
        var discountWay = $('input:radio[name="discountWay"]:checked').val();
        if(shareWay === undefined){
            $('#vip-init-info').text(lang('vip.checkShare'));
            return false;
        }
        if(levelWay === undefined){
            $('#vip-init-info').text(lang('vip.checkUpgrade'));
            return false;
        }
        if(discountWay === undefined){
            $('#vip-init-info').text(lang('vip.checkDiscount'));
            return false;
        }
        $('#init-vip-level').data('shareWay',shareWay);
        $('#init-vip-level').data('levelWay',levelWay);
        $('#init-vip-level').data('discountWay',discountWay);
        $('#vip-init-info').text('');
        $('#confirm-vip-init').text('');
    })
    $('#cancel-init-vip').click(function () {
        $('#confirmVipInitModal').modal('hide');
    })
    $('#init-vip-level').on('click', function(e) {
        e.preventDefault();
        var me = $(this);
        var params = {
            hotel: window.currentHotel,
            share: me.data('shareWay'),
            level: me.data('levelWay'),
            discount: me.data('discountWay'),
        };
        ajaxRequest('/Home/Customer/vipInit', params, 'post', null, function(error, submitBtn, defaultErrorHandler) {
            var errorInfo = error.responseJSON.info;
            $('#confirm-vip-init').text(errorInfo);
        }, me);
    });
})